package DBLayer;

import ModelLayer.*;

import java.sql.*;
import java.util.ArrayList;

public class DBPerson implements DBPersonIF 

{
	private Connection con;
	
	public DBPerson()
	{
		con = DBConnection.getInstance().getDBcon();
	}
	
	public ArrayList<Person> getAllPersons(boolean retriveAssociation)
    {
        return miscWhere("", retriveAssociation);
    }

    public Person searchPersonPhoneNr(String phoneNr, boolean retriveAssociation)
    {   
    	String wClause = "PhoneNr = '" + phoneNr + "'";
        return singleWhere(wClause, retriveAssociation);
    }
    
    public Person searchPersonMobileNr(String mobileNr, boolean retriveAssociation)
    {   
    	String wClause = "MobileNr = '" + mobileNr + "'";
        return singleWhere(wClause, retriveAssociation);
    }
    
    private ArrayList<Person> miscWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		ArrayList<Person> personList = new ArrayList<Person>();	
		
		String query =  buildQuery(wClause);
  
  		try
  		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
			while( results.next() )
			{
				Person pObj = new Person();
				pObj = buildPerson(results);	
				personList.add(pObj);	
			}
			stmt.close();
		}
	 	catch(Exception e)
	 	{
	 		System.out.println("Query exception - select: "+e);
			e.printStackTrace();
	 	}
		return personList;
	}
    
    private Person singleWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		Person pObj = new Person();
	    String query = buildQuery(wClause);
//        System.out.println(query);
		try
		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() )
	 		{
	 			pObj = buildPerson(results);
	 			stmt.close();
			}
		}	
	 	catch(Exception e)
	 	{
	 		System.out.println("Query exception: "+e);
	 	}
		return pObj;
	}

	private String buildQuery(String wClause)
	{
	    String query="SELECT * FROM Person";
		if (wClause.length()>0)
			query=query+" WHERE "+ wClause;		
		return query;
	}

	private Person buildPerson(ResultSet results)
    {   
		Person pObj = new Person();
        try
        { /* PersonID, FirstName, LastName, Address, PostalCode, City, PhoneNr, MobileNr, Email */
            pObj.setPersonID(results.getInt("PersonID"));
            pObj.setfName(results.getString("FirstName"));
            pObj.setlName(results.getString("LastName"));
            pObj.setAddress(results.getString("Address"));
            pObj.setPostalCode(results.getInt("PostalCode"));
            pObj.setCity(results.getString("City"));
            pObj.setPhoneNr(results.getString("PhoneNr"));
            pObj.setMobileNr(results.getString("MobileNr"));
            pObj.setEmail(results.getString("Email"));
        }
        catch(Exception e)
        {
            System.out.println("error in building the Person object");
        }
        return pObj;
    }
}
